<?php

namespace App\Http\Controllers\User;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Hash;
use App\User;

class UserController extends Controller
{
    /**
     * 用户注册
     */
    public function getRegister()
    {
        return view('auth.register');
    }

    /**
     * 验证注册
     */
    public function postRegister(Request $request)
    {
        $this->validate($request, [
            'username' => 'required|unique:users,username|regex:/^\w{6,18}$/',
            'password' => 'required|regex:/^\S{6,18}$/',
            'email'=>'required|email'
        ],[
            'username.required'=>'用户名必填',
            'username.unique'=>'用户名已经存在',
            'username.regex'=>'用户名格式不正确',
            'password.required'=>'密码必填',
            'password.regex'=>'密码格式不正确',
            'email.required'=>'邮箱号必填',
            'email.regex' => '邮箱不正确'
        ]);
        $data = $request->except('_token','repassword');
        
        $user = new User;
        $user -> username = $request->username;
        $user -> password = Hash::make($request->input('password'));
        $user -> email = $request->email;
        $user -> status = 1;
        // dd($user->age);

        if($user->save()){
           return redirect('/login')->with('info','注册成功,请登录');
        }else{
            return back();
        }
        
    }
}
